﻿using System;
using System.Collections.Generic;
using System.Web.Services;
using ClientTools.Library.DataTypes.Logging;
using ClientTools.Library.DataTypes.Umbraco;
using ClientTools.Library.DataTypes.Versions;
using ClientTools.Library.Manager;

namespace ClientTools.Server.Webservices
{
    /// <summary>
    /// Summary description for ClientToolsWebServices
    /// </summary>
    [WebService(Namespace = "http://www.codeplex.com/ClientTools4Umbraco/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class ClientToolsWebServices : ClientToolsBaseService
    {

        #region Logging

        /// <summary>
        /// Gets the log infos.
        /// </summary>
        /// <param name="userName">Name of the user.</param>
        /// <param name="password">The password.</param>
        /// <returns></returns>
        [WebMethod]
        public LogInfoCollection GetLogInfos(string userName, string password)
        {
            Authenticate(userName, password, ServiceTypeEnum.MaintenanceService);
            return LogManager.GetLogInfos();
            //return this.GetLogInfos(userName, password);
        }

        /// <summary>
        /// Gets all logs.
        /// </summary>
        /// <param name="userName">Name of the user.</param>
        /// <param name="password">The password.</param>
        /// <param name="startDate">The start date.</param>
        /// <param name="endDate">The end date.</param>
        /// <param name="logHeader">The log header.</param>
        /// <param name="userId">The user ID.</param>
        /// <param name="nodeId">The node ID.</param>
        /// <param name="maxRows">The max rows.</param>
        /// <param name="comment">The comment</param>
        /// <returns></returns>
        [WebMethod]
        public LogCollection GetLogs(string userName, string password, DateTime? startDate, DateTime? endDate, string logHeader, int? userId, int? nodeId, int? maxRows, string comment)
        {
            Authenticate(userName, password, ServiceTypeEnum.MaintenanceService);
            return LogManager.GetLogs(startDate, endDate, logHeader, userId, nodeId, maxRows, comment);
            //return logManager.GetLogs(startDate, endDate, logHeader, userId, nodeId, maxRows, comment);
        }

        /// <summary>
        /// Deletes the logs by id.
        /// </summary>
        /// <param name="userName">Name of the user.</param>
        /// <param name="password">The password.</param>
        /// <param name="logIds">The log IDs.</param>
        /// <returns></returns>
        [WebMethod]
        public int DeleteLogsById(string userName, string password, List<int> logIds)
        {
            Authenticate(userName, password, ServiceTypeEnum.MaintenanceService);
            return LogManager.DeleteLogsByIDs(logIds.ToArray());
        }

        /// <summary>
        /// Deletes the logs by header.
        /// </summary>
        /// <param name="userName">Name of the user.</param>
        /// <param name="password">The password.</param>
        /// <param name="logHeader">The log header.</param>
        /// <returns></returns>
        [WebMethod]
        public int DeleteLogsByHeader(string userName, string password, string logHeader)
        {
            Authenticate(userName, password, ServiceTypeEnum.MaintenanceService);
            return LogManager.DeleteLogsByHeader(logHeader);
        }

        /// <summary>
        /// Deletes the logs by date.
        /// </summary>
        /// <param name="userName">Name of the user.</param>
        /// <param name="password">The password.</param>
        /// <param name="date">The date.</param>
        /// <returns></returns>
        [WebMethod]
        public int DeleteLogsByDate(string userName, string password, DateTime date)
        {
            Authenticate(userName, password, ServiceTypeEnum.MaintenanceService);
            return LogManager.DeleteLogsByDate(date);
        }

        #endregion

        #region Medias

        [WebMethod]
        public NodeCollection GetRootMedias(string userName, string password, bool recursive)
        {
            Authenticate(userName, password, ServiceTypeEnum.MediaService);
            return UmbracoUser.StartMediaId == -1
                       ? MediaManager.GetRootMedias(recursive)
                       : MediaManager.GetMediasFromNode(UmbracoUser.StartMediaId, recursive);
        }

        [WebMethod]
        public NodeCollection GetMedias(string userName, string password, int parentMediaId, bool recursive)
        {
            Authenticate(userName, password, ServiceTypeEnum.MediaService);
            return UmbracoUser.StartMediaId == -1
                       ? MediaManager.GetRootMedias(recursive)
                       : MediaManager.GetMediasFromNode(UmbracoUser.StartMediaId, recursive);
        }

        [WebMethod]
        public bool DeleteMedia(string userName, string password, int mediaId)
        {
            Authenticate(userName, password, ServiceTypeEnum.MediaService);
            return MediaManager.DeleteMedia(mediaId);
        }

        [WebMethod]
        public Node CreateImage(string userName, string password, int parentMediaId, string fileName, byte[] content)
        {
            Authenticate(userName, password, ServiceTypeEnum.MediaService);
            return MediaManager.CreateNewImage(UmbracoUser, parentMediaId, fileName, content);
        }

        [WebMethod]
        public Node CreateFile(string userName, string password, int parentMediaId, string fileName, byte[] content)
        {
            Authenticate(userName, password, ServiceTypeEnum.MediaService);
            return MediaManager.CreateNewFile(UmbracoUser, parentMediaId, fileName, content);
        }

        [WebMethod]
        public Node CreateFolder(string userName, string password, int parentMediaId, string folderName)
        {
            Authenticate(userName, password, ServiceTypeEnum.MediaService);
            return MediaManager.CreateNewFolder(UmbracoUser, parentMediaId, folderName);
        }

        #endregion

        #region Versions

        [WebMethod]
        public VersionInfo GetVersionInfos(string userName, string password)
        {
            Authenticate(userName, password, ServiceTypeEnum.MaintenanceService);
            return VersionManager.GetVersionInfos();
        }

        //[WebMethod]
        //public VersionInfo GetVersionInfos(string userName, string password, int nodeId)
        //{
        //    Authenticate(userName, password, ServiceTypeEnum.MaintenanceService);
        //    return versionManager.GetVersionInfos(nodeId);
        //}

        [WebMethod]
        public VersionInfo GetVersionInfosFromNode(string userName, string password, int nodeId, bool includeChildren)
        {
            Authenticate(userName, password, ServiceTypeEnum.MaintenanceService);
            return VersionManager.GetVersionInfos(nodeId, includeChildren);

        }

        [WebMethod]
        public VersionInfo GetChildrenVersionInfos(string userName, string password, int nodeId)
        {
            Authenticate(userName, password, ServiceTypeEnum.MaintenanceService);
            return VersionManager.GetChildrenVersionInfos(nodeId);
        }

        [WebMethod]
        public VersionCollection GetVersions(string userName, string password, int nodeId)
        {
            Authenticate(userName, password, ServiceTypeEnum.MaintenanceService);
            return VersionManager.GetVersions(nodeId);
        }

        [WebMethod]
        public VersionInfo DeleteVersion(string userName, string password, Guid guid)
        {
            Authenticate(userName, password, ServiceTypeEnum.MaintenanceService);
            return VersionManager.DeleteVersion(guid);
        }

        [WebMethod]
        public VersionInfo DeleteVersionsByDate(string userName, string password, DateTime date)
        {
            Authenticate(userName, password, ServiceTypeEnum.MaintenanceService);
            return VersionManager.DeleteVersionsByDate(date);
        }

        [WebMethod]
        public VersionInfo DeleteNodeVersionsByDate(string userName, string password, DateTime date, int nodeId)
        {
            Authenticate(userName, password, ServiceTypeEnum.MaintenanceService);
            return VersionManager.DeleteVersionsByDate(date, nodeId);
        }

        [WebMethod]
        public VersionInfo DeleteVersionsByItems(string userName, string password, int numberOfItems)
        {
            Authenticate(userName, password, ServiceTypeEnum.MaintenanceService);
            return VersionManager.DeleteVersionsByNumberOfItems(numberOfItems);
        }

        [WebMethod]
        public VersionInfo DeleteNodeVersionsByItems(string userName, string password, int numberOfItems, int nodeId)
        {
            Authenticate(userName, password, ServiceTypeEnum.MaintenanceService);
            return VersionManager.DeleteVersionsByNumberOfItems(numberOfItems, nodeId);
        }

        #endregion

        #region Nodes

        [WebMethod]
        public NodeCollection GetNodes(string userName, string password, int parentId, bool recursive)
        {
            Authenticate(userName, password, ServiceTypeEnum.MaintenanceService);
            return NodeManager.GetNodes(parentId, recursive);
        }

        #endregion

    }
}
